package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.pojo.Blog;
import com.blog.pojo.Thumb;
import com.blog.pojo.User;
import com.blog.service.BlogService;
import com.blog.service.ThumbService;
import com.blog.mapper.ThumbMapper;
import com.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author DELL
 * @description 针对表【t_thumb】的数据库操作Service实现
 * @createDate 2022-07-13 15:02:46
 */
@Service
public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
        implements ThumbService {

    @Autowired
    private BlogService blogService;

    @Autowired
    private UserService userService;

    /**
     * 统计该博客的点赞数
     *
     * @param blogId
     * @return Integer
     */
    @Override
    public Integer countTheBlogThumbs(Long blogId) {
        LambdaQueryWrapper<Thumb> thumbLambdaQueryWrapper = new LambdaQueryWrapper<>();
        thumbLambdaQueryWrapper.eq(Thumb::getBlogId, blogId);
        return this.count(thumbLambdaQueryWrapper);
    }

    /**
     * 统计用户所有的点赞数
     *
     * @param userId
     * @return Integer
     */
    @Override
    public Integer countTheUserAllThumbs(Long userId) {
        //先根据用户的Id查询其下所有blog
        LambdaQueryWrapper<Blog> blogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blogLambdaQueryWrapper.eq(Blog::getUserId, userId);
        List<Blog> blogList = blogService.list(blogLambdaQueryWrapper);
        //对每个blog进行countTheBlogThumbs操作（根据blogId统计博客的点赞数）
        int total = 0;
        for (Blog blog : blogList) {
            total += this.countTheBlogThumbs(blog.getBlogId());
        }
        return total;
    }
}




